package com.cleaningcloud.erp.service.impl;

import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import com.cleaningcloud.erp.api.domain.ErpCommodityStockAlter;
import com.cleaningcloud.erp.domain.dto.StatisticsQueryDto;
import com.cleaningcloud.erp.domain.statistics.*;
import com.cleaningcloud.erp.domain.vo.ErpCommodityStockAlterVo;
import com.cleaningcloud.erp.mapper.ErpCommodityStockAlterMapper;
import com.cleaningcloud.erp.mapper.ErpWareAllocateMapper;
import com.cleaningcloud.erp.mapper.StatisticsMapper;
import com.cleaningcloud.erp.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Autowired
    private ErpCommodityStockAlterMapper erpCommodityStockAlterMapper;

    @Autowired
    private ErpWareAllocateMapper erpWareAllocateMapper;


    @Override
    public List<ProcureStatistics> procureStatistics(ProcureStatistics procureStatistics) {
        //procureStatistics.setSupplierCommodityType(CommunityTypeConversionUtils.conversionRegexp(procureStatistics.getSupplierCommodityType()));
        if(null != procureStatistics.getSupplierCommodityType()){
            procureStatistics.setSupplierCommodityType(procureStatistics.getSupplierCommodityType().replace('|',','));
        }
        List<ProcureStatistics> list = statisticsMapper.procureStatistics(procureStatistics);
        for (ProcureStatistics statistics : list) {
            statistics.setSupplierCommodityType(CommunityTypeConversionUtils.conversion(statistics.getSupplierCommodityType()));
        }
        return list;
    }

    @Override
    public List<SaleStatistics> saleStatistics(SaleStatistics saleStatistics) {
        //saleStatistics.setCommodityType(CommunityTypeConversionUtils.conversionRegexp(saleStatistics.getCommodityType()));
        if(null != saleStatistics.getCommodityType()){
            saleStatistics.setCommodityType(saleStatistics.getCommodityType().replace('|',','));
        }
        List<SaleStatistics> list = statisticsMapper.saleStatistics(saleStatistics);
        for (SaleStatistics statistics : list) {
            statistics.setCommodityType(CommunityTypeConversionUtils.conversion(statistics.getCommodityType()));
        }
        return list;
    }

    @Override
    public List<OutboundStatistics> outboundStatistics(OutboundStatistics outboundStatistics) {
        return statisticsMapper.outboundStatistics(outboundStatistics);
    }

    @Override
    public List<WarehousingStatistics> warehousingStatistics(WarehousingStatistics warehousingStatistics) {
        //warehousingStatistics.setSupplierCommodityType(CommunityTypeConversionUtils.conversionRegexp(warehousingStatistics.getSupplierCommodityType()));
        if(null != warehousingStatistics.getSupplierCommodityType()){
            warehousingStatistics.setSupplierCommodityType(warehousingStatistics.getSupplierCommodityType().replace('|',','));
        }
        List<WarehousingStatistics> list = statisticsMapper.warehousingStatistics(warehousingStatistics);
        for (WarehousingStatistics statistics : list) {
            statistics.setSupplierCommodityType(CommunityTypeConversionUtils.conversion(statistics.getSupplierCommodityType()));
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> wareStatistics(StatisticsQueryDto statisticsQueryDto) {
        return statisticsMapper.wareStatistics(statisticsQueryDto);
    }


    @Override
    public List<StockStatistics> stockStatistics(StatisticsQueryDto statisticsQueryDto) {
        List<StockStatistics> list = statisticsMapper.stockStatistics(statisticsQueryDto);
        ErpCommodityStockAlter erpCommodityStockAlter = new ErpCommodityStockAlter();
        erpCommodityStockAlter.setParams(statisticsQueryDto.getParams());
        for (StockStatistics statistics : list) {
            erpCommodityStockAlter.setCommodityId(statistics.getCommodityId());
            erpCommodityStockAlter.setWareId(statisticsQueryDto.getWareId());
            ErpCommodityStockAlterVo erpCommodityStockAlterVo = erpCommodityStockAlterMapper.selectErpCommodityStockAlterStatics(erpCommodityStockAlter);
            if(erpCommodityStockAlterVo != null) {
                statistics.setStartNumber(erpCommodityStockAlterVo.getTotalNumber());
                statistics.setStartPrice(statistics.getCostPrice().multiply(BigDecimal.valueOf(erpCommodityStockAlterVo.getTotalNumber())));
                statistics.setRealityNumber(erpCommodityStockAlterVo.getRkNumber());
                statistics.setRealityPrice(statistics.getCostPrice().multiply(BigDecimal.valueOf(erpCommodityStockAlterVo.getRkNumber())));
                statistics.setAllocateNumber(erpWareAllocateMapper.getDbNumber(erpCommodityStockAlter) == null ? 0 : erpWareAllocateMapper.getDbNumber(erpCommodityStockAlter));
                statistics.setConsumeNumber(erpCommodityStockAlterVo.getXhNumber());
                statistics.setSaleNumber(Math.abs(erpCommodityStockAlterVo.getCkNumber()));
                statistics.setSalePrice(statistics.getCostPrice().multiply(BigDecimal.valueOf(Math.abs(erpCommodityStockAlterVo.getCkNumber()))));
            }

        }
        return list;
    }

    @Override
    public Map<String, Object> stockHeadStatistics() {
        return statisticsMapper.stockHeadStatistics();
    }
}
